﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Automation.Peers;
using System.Windows.Automation;
using UnitTestUtils.UI;
using System.Windows.Controls;
using System.Windows.Automation.Provider;
using AppToolkit.WPF.UI;

namespace AppToolkit.WPF.Tests.UI
{
    [TestFixture]
    public class ErrorReportDialog_interactive
    {
        UIHost _host;
        ErrorReportDialog _target;

        #region Setup / Teardown
        [SetUp]
        public void Setup()
        {
            _target = new ErrorReportDialog();
            _host = UIHost.Create(_target);
        }

        [TearDown]
        public void Dispose()
        {
            _target = null;
            _host = null;
            UnitTestUtils.UI.UITestHelper.CleanupDispatcher();
        } 
        #endregion

        [Test, RequiresSTA, Explicit]
        public void InteractiveTest_real_Exception()
        {
            try
            {
                throw new ArgumentException("This is a test exception!");
            }
            catch (Exception ex)
            {
                _target.Exception = ex;
                _target.ShowDialog();    
            }
        }

        [Test, RequiresSTA, Explicit]
        public void InteractiveTest_check_resizing_for_long_callstacks()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < 30; i++)
                {
                    sb.AppendLine("This is a very long outer Message.");
                }
                new ExceptionHelper().Call(sb.ToString());
            }
            catch (Exception ex)
            {
                _target.Exception = ex;
                _target.ShowDialog();
            }
        }

        [Test, RequiresSTA, Explicit]
        public void InteractiveTest_navigate_ExceptionStack()
        {
            string someText = "This is example text to make the exception message longer!";
            try
            {
                try
                {
                    throw new ArgumentException("Inner Exception:\r\n" + someText);
                }
                catch (Exception ex0)
                {
                    throw new InvalidOperationException("Outer Exception\r\n" + someText, ex0);
                }
            }
            catch (Exception ex)
            {
                _target.Exception = ex;
                _target.ShowDialog();
            }
        }

        class ExceptionHelper
        {
            int _count = 50;

            public int Count
            {
                get { return _count; }
                set { _count = value; }
            }

            public void Call(string msg)
            {
                while (_count-- > 0)
                {
                    Call(msg);
                }
                throw new Exception(msg);
            }

            public void Call()
            {
                Call("Some internal exception!");
            }
        }
    }
}
